package com.cip.admin.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.omg.PortableInterceptor.SUCCESSFUL;

import com.cip.master.model.ClassYear;
import com.cip.master.model.Section;
import com.cip.master.service.ClassYearService;
import com.cip.master.service.ExaminationService;
import com.cip.master.service.SectionService;
import com.cip.webapp.action.BaseAction;
import com.cip.admin.service.EmailService;
import com.cip.admin.service.ImportFileService;
import com.cip.admin.service.UserManager;
import com.cip.admin.model.Email;
import com.cip.admin.model.User;


public class EmailAction extends BaseAction {
	
	private EmailService emailService;
	private Email email;
	private File fileUpload;
	private ImportFileService importFileService;
	private UserManager userManager;
	private ClassYearService classYearService;
	private SectionService sectionService;
	
	private List<ClassYear> classYears;
	private List<Section> sectionList; 
	private String classYear;
	private String sectionName;
	
	public String showEmail() throws Exception {
		return SUCCESS;
	}
	
	public String showBulkEmail() throws Exception{
		return SUCCESS;
	}
	
	public String showBroadcastEmail() throws Exception{
		setClassYears(classYearService.getAllClassYear());
		setSectionList(sectionService.getAllSection());
		return SUCCESS;
	}
	
	public String sendGroupEmail() throws Exception{
		System.out.println("In Action=====Email");
		emailService.sendEmail(email.getRecipient(), email.getBody(), email.getSubject());
		return SUCCESS;
		
	}
	
	public String sendBulkEmail() throws Exception{
		List<Map<String, String>> csvValues = importFileService.getDataFromCSV(fileUpload);
		for (Map<String, String> map : csvValues) {
			emailService.sendEmail(map.get("Email"), email.getBody(), email.getSubject());
		}
		return SUCCESS;
	}
	
	public String broadcastEmail() throws Exception{
		List<User> userList = userManager.getAllUsers();
		for (User user : userList) {
			emailService.sendEmail(user.getEmail(), email.getBody(), email.getSubject());
		}
		
		return SUCCESS;
	}

	public EmailService getEmailService() {
		return emailService;
	}

	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	public ImportFileService getImportFileService() {
		return importFileService;
	}

	public void setImportFileService(ImportFileService importFileService) {
		this.importFileService = importFileService;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public ClassYearService getClassYearService() {
		return classYearService;
	}

	public void setClassYearService(ClassYearService classYearService) {
		this.classYearService = classYearService;
	}

	public SectionService getSectionService() {
		return sectionService;
	}

	public void setSectionService(SectionService sectionService) {
		this.sectionService = sectionService;
	}

	public List<ClassYear> getClassYears() {
		return classYears;
	}

	public void setClassYears(List<ClassYear> classYears) {
		this.classYears = classYears;
	}

	public List<Section> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<Section> sectionList) {
		this.sectionList = sectionList;
	}

	public String getClassYear() {
		return classYear;
	}

	public void setClassYear(String classYear) {
		this.classYear = classYear;
	}

	public String getSectionName() {
		return sectionName;
	}

	public void setSectionName(String sectionName) {
		this.sectionName = sectionName;
	}	
}
